using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Xml;
using System.Xml.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;

public class GameIO : MonoBehaviour {

	// Use this for initialization
	void Start () {
		
	}
	
	// Update is called once per frame
	void Update () {
	
	}
	
	//Check file is exist or not
	static public bool isKeyExist (string keyName) {
		
		string key = PlayerPrefs.GetString(keyName);
        
		if(key == "") {
			return false;
		}
		else {
			return true;
		}
	}
	
	//Save data
	static public void SaveData (string keyName, string root, string element, string newvalue)
	{
		string strData = InitNewData(keyName, root, element, newvalue);
		WriteResourceFile(strData, keyName);
	}

	static public void WriteResourceFile (string strData, string keyName) {
		
		PlayerPrefs.SetString(keyName, strData);
	}
	
	static public string ReadData (string keyName) {
		return PlayerPrefs.GetString(keyName);
	}
	
	static public string ReadDataFromKey(string keyName, string root, string element) {
		
		string result = "";
		
		//Load XML data from key
		string data = PlayerPrefs.GetString(keyName);
        XDocument xdocs = XDocument.Parse(data);
		result = LoadElement(xdocs, root, element);
		
		return result;
	}
	
	static public string ReadDataFromString(string xml, string root, string element) {
		
		string result = "";
		//Parse string data to XML Document to load
        XDocument xdocs = XDocument.Parse(xml);
		result = LoadElement(xdocs, root, element);
		return result;
		
	}
	
	static public string LoadElement(XDocument xdocs, string root, string element)
    {
		
        string result = "";
		
        var query = from c in xdocs.Elements(root).Elements(element)
                    select c;

		//Search for all target element
        foreach (XElement xelement in query)
        {
            result = xelement.Value;
        }
		
		return result;
    }
	
    static public string InitNewData(string keyName, string root, string element, string newval)
    {
        //Load current xml file
		string data = ReadData(keyName);
        XDocument xmlFile = XDocument.Parse(data);
		
		List<XElement> list = (from c in xmlFile.Elements(root).Elements(element)
                    select c).ToList();
		
		if(list.Count > 0) {
	        //Replace existed value for target element
	        foreach (XElement xelement in list)
	        {
	            xelement.Value = newval;
	        }
		}
		else {
			XElement newElement = new XElement(element,newval);
			xmlFile.Root.Add(newElement);
		}
		
        //Save modified xml to string
        StringBuilder strBuilder = new StringBuilder();
        TextWriter textWriter = new Utf8StringWriter(strBuilder);
		xmlFile.Declaration = new XDeclaration("1.0", "utf-8", null);
		xmlFile.Save(textWriter);
		
        return textWriter.ToString();

    }
}

public class Utf8StringWriter : StringWriter
{
	public Utf8StringWriter(StringBuilder sb) : base (sb)
	{
		
	}
	
    public override Encoding Encoding {
		get {
			return Encoding.UTF8;
		}
	}
}
